<?php


namespace app\modules\content\controllers;


use app\libs\ApiControl;
use app\libs\GetData;
use app\modules\content\models\Info;
use app\modules\content\models\Information;
use yii\data\Pagination;
use yii\helpers\Url;

class InformationController extends ApiControl
{
    public $enableCsrfValidation = false;
    public function actionIndex(){
        $params = \Yii::$app->request->get();
        $belong = empty($params['belong'])?1:$params['belong'];
        $page_index = empty($params['page'])?1:$params['page'];
        $model = Information::find();
        $query = $model->asArray()->where(['belong'=>$belong,'info_id'=>0]);
        $count = $query->count();
        $page = new Pagination(['pageSize'=>10,'totalCount'=>$count]);
        $res = $query->offset($page->offset)->limit($page->limit)->orderBy(['sort'=>SORT_DESC,'id'=>SORT_DESC])->all();
        return $this->render('index',[
            'list'=>\Yii::$app->params['spider_lists'],
            'content'=>$res,
            'belong'=>$belong,
            'page_index'=>$page_index,
            'page'  =>  $page
        ]);
    }


    public function actionDetail(){
        $params = \Yii::$app->request->get();
        if (empty($params['id']))return "id不能为空";
        $id = $params['id'];
        $information = Information::findOne(['id'=>$id]);

        return $this->render('detail',[
            'page'=>isset($params['page'])?$params['page']:1,
            'belong'=>isset($params['belong'])?$params['belong']:1,
            'cates' => Info::$cate,
            'information'=>$information
        ]);
    }

    /**
     * Notes:第三方资讯 编辑
     * User: monster
     * DateTime: 2021/11/11 17:02
     */
    public function actionEdit(){
        $params = \Yii::$app->request->post();
        $page_index = empty($params['page'])?1:$params['page'];
        $belong = empty($params['belong'])?1:$params['belong'];
        $getData = new GetData();
        $must = ['name'=>"标题",'cate'=>"分类",'content'=>"内容"];
        $data = $getData->PostData($must,'info');
        $id = $data['id'];
        unset($data['id']);
        unset($data['createTime']);
        unset($data['belong']);
        unset($data['page']);
        $data['edit']=\Yii::$app->session->get("userName");
        $data['pushTime'] = strtotime($data['pushTime']);
        $res = Information::updateAll($data,['id'=>$id]);
        if (empty($res)){
            echo "<script>alert('操作失败');setTimeout(function(){window.history.back();},1000)</script>";
        } else{
            echo "<script>alert('操作成功');setTimeout(function(){location.href='".Url::to(['information/index','page'=>$page_index,'belong'=>$belong])."'},1000)</script>";
        }
    }

    /**
     * Notes:第三方资讯 提交
     * User: monster
     * DateTime: 2021/11/11 17:02
     * @return string
     */
    public function actionSub(){
        $params = \Yii::$app->request->post();
         $page_index = empty($params['page'])?1:$params['page'];
         $belong = empty($params['belong'])?1:$params['belong'];
        $getData = new GetData();
        $must = ['name'=>"标题",'cate'=>"分类",'content'=>"内容"];
        $data = $getData->PostData($must,'info');
        $id = $data['id'];
        unset($data['id']);
        unset($data['createTime']);
        unset($data['belong']);
        unset($data['page']);
        $data['edit']=\Yii::$app->session->get("userName");
        $data['pushTime'] = strtotime($data['pushTime']);
        $res = Information::updateAll($data,['id'=>$id]);
        $informationModel = Information::findOne(['id'=>$id])->toArray();
        $infoModel = new Info();
        $infoModel->name = $informationModel['name'];
        $infoModel->pic = $informationModel['pic'];
        $infoModel->keywords = $informationModel['keywords'];
        $infoModel->summary = $informationModel['summary'];
        $infoModel->content = $informationModel['content'];
        $infoModel->cate = $informationModel['cate'];
        $infoModel->author = $informationModel['author'];
        $infoModel->sort = $informationModel['sort'];
        $infoModel->createTime = time();
        $infoModel->viewCount = rand(200,500);
        $infoModel->isShow = 1;
        $infoModel->isTop = $informationModel['isTop'];
        $infoModel->isPush = 1;
        $infoModel->pushTime = $informationModel['pushTime'];
        $infoModel->save();
        Information::updateAll(['info_id'=>$infoModel->id],['id'=>$id]);
        echo "<script>alert('操作成功');setTimeout(function(){location.href='".Url::to(['information/index','page'=>$page_index,'belong'=>$belong])."'},1000)</script>";
    }
}